package cn.hiapi.shopkeeper.service.activity;

import cn.hiapi.basic.ResUtils;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.service.AbsBasicService;
import cn.hiapi.shopkeeper.entity.activity.ActivityLotteryItem;
import cn.hiapi.shopkeeper.jpa.activity.ActivityLotteryItemJpa;
import cn.hiapi.utils.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * @author AdinZ
 * @date 2022/12/13 0:35
 */
@Service
public class ActivityLotteryItemService extends AbsBasicService<ActivityLotteryItemJpa, ActivityLotteryItem, Long> {

    @Transactional
    public int deleteByAid(long aid) {
        return ResUtils.getDefault(this.repository.deleteByAid(aid));
    }


    /**
     * 获取奖品列表
     *
     * @param aid
     * @return
     */
    public List<ActivityLotteryItem> getByAidRandom(long aid) {
        List<ActivityLotteryItem> items = this.findList(new QueryWrapper().eq("aid", aid));
        items.add(new ActivityLotteryItem() {{
            this.setType(9);
            this.setId(9L);
            this.setItemId(9L);
        }});
        int i1 = 8 - items.size();
        for (int i = 0; i < i1; i++) {
            ActivityLotteryItem item = new ActivityLotteryItem();
            item.setType(RandomUtils.choice(new int[]{1, 9}));
            if (item.getType() != 9)
                item.setPrice(BigDecimal.valueOf(RandomUtils.randomInt(1, 20)));
            items.add(item);
        }
        Collections.shuffle(items);
        return items;
    }

    public List<ActivityLotteryItem> findByAid(long aid) {
        List<ActivityLotteryItem> items = this.findList(new QueryWrapper().eq("aid", aid));
        return items;
    }
}
